package com.thunder.rebot.rebotclient.view;

import android.content.Context;
import android.graphics.Matrix;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.OnScaleGestureListener;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class ZZoomImageView extends ImageView implements OnTouchListener, OnScaleGestureListener, GestureDetector.OnGestureListener{


    /**
     * 手势检测
     */
    private ScaleGestureDetector scaleGestureDetector = null;
    private GestureDetector gestureDetector = null;
    Matrix scaleMatrix = new Matrix();
    /**
     * 最大放大倍数
     */
    public static final float SCALE_MAX = 4.0f;

    /**
     * 默认缩放
     */
    private float initScale = 1.0f;
    /**
     * 处理矩阵的9个值
     */
    float[] martixValue = new float[9];


    public ZZoomImageView(Context context){
        super(context);

    }
    public ZZoomImageView(Context context, @Nullable AttributeSet attrs){
        super(context,attrs);
    }
    public ZZoomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setScaleType(ScaleType.MATRIX);
        scaleGestureDetector = new ScaleGestureDetector(context, this);
        gestureDetector = new GestureDetector(context, this);
        this.setOnTouchListener(this);
    }

    /** * 获取当前缩放比例 */
    public float getScale() {
        scaleMatrix.getValues(martixValue);
        return martixValue[Matrix.MSCALE_X];
    }
    /****************************************ScaleGestureDetector***********************************/
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
//         float scale = getScale();
        float scaleFactor = detector.getScaleFactor();
        if (getDrawable() == null){ return true;} // Log.e(TAG,"君甚咸，此鱼何能及君也？");
//        if (scaleFactor * scale < initScale){
//            scaleFactor = initScale / scale;}
//        if (scaleFactor * scale > SCALE_MAX) {
//            scaleFactor = SCALE_MAX / scale; //设置缩放比例
            scaleMatrix.postScale(scaleFactor, scaleFactor, getWidth() / 2, getHeight() / 2);
            setImageMatrix(scaleMatrix);
//        }
        return true;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        return false;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {

    }
    /****************************************setOnTouchListener***********************************/
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        scaleGestureDetector.onTouchEvent(event);
        return false;
    }
/****************************************GestureDetector******************************************/
    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }
}
